package com.test.mchelepesa.bean_rice.config_info_rice;

import android.text.TextUtils;

import com.test.mchelepesa.constant_rice.Constant_rice;
import com.test.mchelepesa.constant_rice.Url_rice;
import com.test.mchelepesa.util_rice.StringUtil_rice;
import com.google.gson.Gson;
import com.test.mchelepesa.bean_rice.StaticConfig_rice;
import com.test.mchelepesa.util_rice.helper_rice.UserInfoHelper_rice;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.TreeMap;

public class GlobalConfig_rice {

    private static class Inner {
        private static GlobalConfig_rice instance = new GlobalConfig_rice();
    }

    private GlobalConfig_rice() {
    }

    public static GlobalConfig_rice getInstance() {
        return Inner.instance;
    }

    public static void parse(JSONObject jsonObject) {
        if (jsonObject == null) return;
        Inner.instance.countryConfigTreeMap.clear();
        Gson gson = new Gson();
        Inner.instance = gson.fromJson(jsonObject.toString(), GlobalConfig_rice.class);

        if (Inner.instance.common == null)
            Inner.instance.common = gson.fromJson(jsonObject.optString(Constant_rice.WordPrefix + "common"), Common.class);

        if (Inner.instance.country == null)
            Inner.instance.country = new ArrayList<>();

        for (String country : Inner.instance.country) {
            Inner.instance.countryConfigTreeMap.put(country, gson.fromJson(jsonObject.optString(country), CountryConfig.class));
        }

    }

    private ArrayList<String> country;
    private Common common;
    private final TreeMap<String, CountryConfig> countryConfigTreeMap = new TreeMap<>();

    private int minVersion;
    private int lastVersion;
    private String lastVersionName;
    private String lastVersionDesc;

    public ArrayList<String> getCountryFullPaths() {
        ArrayList<String> list = new ArrayList<>();
        for (String country : getCountries()) {
            list.add(Url_rice.Url + "/" + country);
        }
        return list;
    }

    public ArrayList<String> getCountries() {
        if (country == null) return new ArrayList<>();
        return country;
    }

    public TreeMap<String, CountryConfig> getCountryConfigTreeMap() {
        return countryConfigTreeMap;
    }

    public String[] getPermissionArr() {
        if (common == null) return new String[0];
        return common.getPermissionArr();
    }

    public String[] getHomeArr() {
        if (common == null) return new String[0];
        return common.getHomeArr();
    }

    public String[] getHomeV2Arr() {
        if (common == null) return new String[0];
        return common.getHomeV2Arr();
    }

    public int getMinVersion(){
        return minVersion;
    }

    public int getLastVersion(){
        return lastVersion;
    }

    public String getLastVersionName(){
        return StringUtil_rice.getSafeString(lastVersionName);
    }

    public String getLastVersionDesc(){
        return StringUtil_rice.getSafeString(lastVersionDesc);
    }

    public String isTestAccount(String mobile) {
        CountryConfig countryConfig = countryConfigTreeMap.get(StaticConfig_rice.Country_Word);
        if (countryConfig == null) return "";
        if (mobile.equalsIgnoreCase(countryConfig.getAccM())) {
            return countryConfig.getAccTk();
        }
        return "";
    }

    public String getCompanyLogo(){
        return getCountryConfig().getLicenseCompanyLogo();
    }

    public String getCompanyName(){
        return getCountryConfig().getLicenseCompanyName();
    }

    public String getTermOfUse(){
        return getCountryConfig().getTermOfUse();
    }

    public CountryConfig getCountryConfig(){
        if (StaticConfig_rice.Country_Config!=null) return StaticConfig_rice.Country_Config;
        try {
            CountryConfig countryConfig = countryConfigTreeMap.get(StaticConfig_rice.Country_Word);
            if (countryConfig!=null) return countryConfig;
        }catch (Exception e){
            StringUtil_rice.printException(e);
            return new CountryConfig();
        }
        return new CountryConfig();
    }

    public boolean isTestAccount() {
        String mobile = UserInfoHelper_rice.getInstance().getMobil_rice();
        return !TextUtils.isEmpty(isTestAccount(mobile));
    }

    /**
     * 是否需要申请UsageStats权限
     */
    public Boolean needRequestUsageStatsPermission() {
        if (common == null) return false;
        return common.needRequestUsageStatsPermission();
    }

    /**
     * 获取查询使用统计的天数
     */
    public int getUsageStatsDays() {
        if (common == null) return 7;
        return common.getUsageStatsDays();
    }

    public static class CountryConfig {

        private String accM;
        private String accTk;
        private String customer;

        private String licenseCompanyLogo;
        private String licenseCompanyName;

        private String termOfUse;

        private CountryConfig() {}

        public String getAccM() {
            return StringUtil_rice.getSafeString(accM);
        }

        public String getAccTk() {
            return StringUtil_rice.getSafeString(accTk);
        }

        public String getCustomer() {
            return StringUtil_rice.getSafeString(customer);
        }

        public String getLicenseCompanyName() {
            return StringUtil_rice.getSafeString(licenseCompanyName);
        }

        public String getTermOfUse() {
            return StringUtil_rice.getSafeString(termOfUse);
        }

        public String getLicenseCompanyLogo() {
            return StringUtil_rice.getSafeString(licenseCompanyLogo);
        }

        @Override
        public String toString() {
            return "CountryConfig{" +
                    "accM='" + accM + '\'' +
                    ", accTk='" + accTk + '\'' +
                    ", customer='" + customer + '\'' +
                    '}';
        }
    }

    public static class Common {

        private ArrayList<String> permissionGp;
        private ArrayList<String> permission;
        private ArrayList<String> home;
        private ArrayList<String> homeV2;
        private String accVersion;


        /**
         * if YES, request PACKAGE_USAGE_STATS permission, or do nothing
         */
        private String usageStatsPermission;
        /**
         * usage stats days
         */
        private int usageStatsDays = 7;

        private Common() {
        }

        public ArrayList<String> getPermissionGp() {
            if (permissionGp == null) return new ArrayList<>();
            return permissionGp;
        }

        public ArrayList<String> getPermission() {
            if (permission == null) return new ArrayList<>();
            return permission;
        }

        public String[] getPermissionArr() {
            if (permission == null) return new String[0];
            String[] result = new String[permission.size()];
            for (int i = 0; i < permission.size(); i++) {
                result[i] = permission.get(i);
            }
            return result;
        }

        public ArrayList<String> getHome() {
            if (home == null) return new ArrayList<>();
            return home;
        }

        public String[] getHomeArr() {
            if (home == null) return new String[0];

            String[] result = new String[home.size()];
            for (int i = 0; i < home.size(); i++) {
                result[i] = home.get(0);
            }
            return result;
        }

        public String[] getHomeV2Arr() {
            if (homeV2 == null) return new String[0];

            String[] result = new String[homeV2.size()];
            for (int i = 0; i < homeV2.size(); i++) {
                result[i] = homeV2.get(i);
            }
            return result;
        }

        public String getAccVersion() {
            return StringUtil_rice.getSafeString(accVersion);
        }

        /**
         * 是否需要申请UsageStats权限
         */
        public boolean needRequestUsageStatsPermission() {
            if (TextUtils.isEmpty(usageStatsPermission)) {
                return false;
            } else {
                return usageStatsPermission.equalsIgnoreCase("YES");
            }
        }

        /**
         * 获取查询使用统计的天数
         */
        public int getUsageStatsDays() {
            return usageStatsDays;
        }

    }
}
